package com.my.myupdata;

import android.Manifest;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.my.myupdata.util.IntentSettingUtil;
import com.my.myupdata.util.MWBPermissionUtil;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
    private TextView tvCurrentVersion;
    private TextView tvServiceVersion;
    private Button btnCheckUpdata;
    private EditText etServiceVersion;
    private String strCurrentVersion;
    private String strServiceVersion;
    private MWBPermissionUtil permissionUtil = new MWBPermissionUtil();
    private AlertDialog.Builder alertDialog;
    private IntentSettingUtil mIntentSettingUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initListener();
        initPermisson();
    }

    private void initView() {
        tvCurrentVersion = findViewById(R.id.tv_current_version);
        tvServiceVersion = findViewById(R.id.tv_service_version);
        btnCheckUpdata = findViewById(R.id.btn_check_update);
        etServiceVersion = findViewById(R.id.et_service_version);
    }

    private void initListener() {
        btnCheckUpdata.setOnClickListener(this);
    }

    private void initPermisson() {
        // 检查是否拥有该权限
        boolean a = permissionUtil.checkSinglePermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (a) {
            Log.i("mwb", "拥有该权限");
        } else {
            Log.i("mwb", "没有该权限");
            alertDialog = new AlertDialog.Builder(this)
                    .setTitle("提示")
                    .setMessage("需要读写sd卡的权限，否则无法为您提供更新服务")
                    .setPositiveButton("立即前往", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            mIntentSettingUtil = new IntentSettingUtil(MainActivity.this);
                            mIntentSettingUtil.jumpPermissionSetting();

                        }
                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                        }
                    });
            alertDialog.create();
            alertDialog.show();


            // 申请权限
            permissionUtil.applySinglePermisson(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_check_update: // 检查更新
                checkUpdata();
                break;
        }
    }

    /**
     * 检查更新
     */
    private void checkUpdata() {
        // 1.获取当前系统的版本号
        // 2.获取服务器上的版本号
        // 3.进行对比
        getService();

    }

    /**
     * 获取版本号
     */
    private void getService() {
//        strCurrentVersion =
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "用户授予了权限", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "用户没有授予权限", Toast.LENGTH_SHORT).show();
                }
                break;
        }

    }
}
